<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/16-15:19
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;


    use app\admin\model\Logs;
    use app\admin\model\Settings;
    use app\common\service\CheckLogin;

    use library\Controller;

    use think\exception\HttpResponseException;
    use think\Response;


    /**
     * Class ChatBase
     * @package app\wechat\controller
     */
    class ChatBase extends Controller
    {


        protected $limit;
        protected $page;

        protected function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            if (!CheckLogin::instance()->isLogin()) {
                return redirect(url("/admin/login/index"));
            }
            $param       = $this->request->param();
            $this->limit = isset($param['limit']) ? $param['limit'] : 20;
            $this->page  = isset($param['page']) ? $param['page'] : 1;
        }



        /**
         * @param $data
         * @param int $code
         * @param string $msg
         * @param int $count
         * @param string $type
         * @param array $header
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/13
         * @name: ajaxResult
         * @describe:
         */
        public function ajaxResult($data, $code = 0, $msg = '', $count = 0, array $header = [])
        {
            $result = [
                'code'  => $code,
                'msg'   => $msg,
                'time'  => time(),
                'data'  => $data,
                'count' => $count,
            ];

            $type     = "json";
            $response = Response::create($result, $type)->header($header);

            throw new HttpResponseException($response);
        }



        /**
         * @param null $title
         * @param null $content
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/6
         * @name: addLog
         * @describe:添加日志
         */
        protected function addLog($title = null, $content = null)
        {
            $log = new Logs();
            $this->setGlobals();
            $log->setTitle($title);
            $log->setContent($content);
            if ($GLOBALS['log'] = 1) {
                $log->record();
            }
        }


        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/13
         * @name: setGlobals
         * @describe:设置全局
         */
        public function setGlobals()
        {
            $setting = new Settings();
            $safe    = $setting->getSetting()['safe'];
            if (isset($safe) && (int)$safe['admin_log'] == 1) { //是否开启网站后台日志
                $GLOBALS['log'] = 1;
            } else {
                $GLOBALS['log'] = 0;
            }
        }

    }